/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import server.MailServer;

/**
 * ListarUsuario representa la interfaz grafica para mostrar los usuario de un servidor.
 * @author Nick
 */
public class ListarUsuario extends javax.swing.JFrame {
    private MailServer servidor;
    private DefaultTableModel modeloTablaUsuarios;
    /**
     * Constructor de la clase ListarUsuario.
     * Creates new form ListarUsuario
     */
    public ListarUsuario(MailServer servidorSeleccionado) {
        initComponents();
        servidor = servidorSeleccionado;
        this.setSize(700, 400);
        this.setLocationRelativeTo(null);
        jtxtServer.setText(servidorSeleccionado.toString());
        jtxtServer.setEditable(false);
        actualizarTablaUsuarios();
    }
    
    /**
     * Actualiza la tabla de usuarios con todos los usuarios registrados en el servidor.
     */
    public void actualizarTablaUsuarios(){
        modeloTablaUsuarios = new DefaultTableModel(new Object[]{"Nombre","Cantidad Mensajes"}, 0);
        jtUsuarios.setModel(modeloTablaUsuarios);
        ArrayList<String> usuarios = servidor.obtenerUsuariosRegistrados();
        int cantidadUsuarios = usuarios.size();       
        for (int i = 0; i < cantidadUsuarios; i++) {
            Object[] datos = new Object[2];
            datos[0] = usuarios.get(i);
            String usuario = usuarios.get(i);
            datos[1] = servidor.obtenerMensajesUsuario(usuario).size();
            modeloTablaUsuarios.addRow(datos);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtUsuarios = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jtxtServer = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Server");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(140, 70, 80, 30);

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton1.setText("Mostrar");
        getContentPane().add(jButton1);
        jButton1.setBounds(70, 120, 90, 40);

        jtUsuarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtUsuarios);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(70, 170, 452, 90);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setText("Listar Usuarios");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(200, 20, 190, 20);
        getContentPane().add(jtxtServer);
        jtxtServer.setBounds(210, 70, 160, 30);

        jPanel1.setBackground(new java.awt.Color(204, 0, 0));
        jPanel1.setLayout(null);

        jButton4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton4.setText("Volver Login");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4);
        jButton4.setBounds(370, 230, 130, 40);

        jButton3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton3.setText("Nuevo");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(260, 230, 100, 40);

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton2.setText("Eliminar");
        jPanel1.add(jButton2);
        jButton2.setBounds(160, 230, 90, 40);

        jButton5.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        jButton5.setText("<-- Atras");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton5);
        jButton5.setBounds(40, 230, 100, 40);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(30, 50, 530, 290);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        Login.formularioCreacionUsuario = new NuevoUsuario(servidor);
        this.setVisible(false);
        Login.formularioCreacionUsuario.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        Login.getInstance().actualizarVista();
        Login.getInstance().setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        Login.formularioServidor.setVisible(true);
    }//GEN-LAST:event_jButton5ActionPerformed
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtUsuarios;
    private javax.swing.JTextField jtxtServer;
    // End of variables declaration//GEN-END:variables
}
